package cn.iocoder.yudao.position.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.position.entity.PositionCategoryDO;
import cn.iocoder.yudao.position.service.PositionCategoryService;
import cn.iocoder.yudao.position.vo.PositionCategoryPageReqVO;
import cn.iocoder.yudao.position.vo.PositionCategoryRespVO;
import cn.iocoder.yudao.position.vo.PositionCategorySaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 字典-行业类别表(树形结构)")
@RestController
@RequestMapping("/position/category")
@Validated
public class PositionCategoryController {

    @Resource
    private PositionCategoryService positionCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建字典-行业类别表(树形结构)")
    //@PreAuthorize("@ss.hasPermission('position:category:create')")
    public CommonResult<Long> createCategory(@Valid @RequestBody PositionCategorySaveReqVO createReqVO) {
        return success(positionCategoryService.createCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新字典-行业类别表(树形结构)")
    //@PreAuthorize("@ss.hasPermission('position:category:update')")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody PositionCategorySaveReqVO updateReqVO) {
        positionCategoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除字典-行业类别表(树形结构)")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('position:category:delete')")
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        positionCategoryService.deleteCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得字典-行业类别表(树形结构)")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('position:category:query')")
    public CommonResult<PositionCategoryRespVO> getCategory(@RequestParam("id") Long id) {
        PositionCategoryDO category = positionCategoryService.getCategory(id);
        return success(BeanUtils.toBean(category, PositionCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得字典-行业类别表(树形结构)分页")
    //@PreAuthorize("@ss.hasPermission('position:category:query')")
    public CommonResult<PageResult<PositionCategoryRespVO>> getCategoryPage(@Valid PositionCategoryPageReqVO pageReqVO) {
        PageResult<PositionCategoryDO> pageResult = positionCategoryService.getCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PositionCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出字典-行业类别表(树形结构) Excel")
    //@PreAuthorize("@ss.hasPermission('position:category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCategoryExcel(@Valid PositionCategoryPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PositionCategoryDO> list = positionCategoryService.getCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "字典-行业类别表(树形结构).xls", "数据", PositionCategoryRespVO.class, BeanUtils.toBean(list, PositionCategoryRespVO.class));
    }

}